var qs = require('querystring');
var user = {};
var isLogin = true;
//该函数用于模拟开发者的用户资料功能,查询单个用户的资料
//输入参数用户id
function process(req,res,db){
    var finish = false;
    var body = '';
    req.on('data', function (data) {
        body += data;
    });
    req.on('end', function () {
        req.finish = true;
        var postBody = qs.parse(body);
        if (postBody.id == null){
            res.writeHead(403,{'Content-Type': 'text/plain','Content-Length': "Missing parameter.".length});
            res.end("Missing parameter.");
            console.log("Missing parameter.");
            return;
        }
        
        if (postBody.option == "changePWD") {
            user.oldpassword = postBody.oldpwd;
            user.password = postBody.newpwd;
            console.log(postBody)
            changePWD(postBody.id, db, res, user, isLogin);
        }else if (postBody.option == "edit") {
            edit(postBody.id, db, res, postBody.user, isLogin);
        }else{
           getProfile(postBody.id,db,res); 
        }
    });
}

function getProfile(id,db,res){ 
    db.get("select id,username,portrait from user where id = ?",id,function(err,row){
        if (err != null){
            res.writeHead(500,{'Content-Type': 'text/plain','Content-Length': "Server error".length});
            res.end("Server error");
            console.log(err);
        }else if (typeof row == "undefined"){
            res.writeHead(500,{'Content-Type': 'text/plain','Content-Length': "Server error".length});
            res.end("Server error");
            console.log(row);
        }else{
            var responseString = JSON.stringify(row[0]);
            res.writeHead(200,{'Content-Type': 'text/plain','Content-Length': Buffer.byteLength(responseString,'utf8')});
            res.end(responseString);
        }
    })
}

function changePWD (id, db, res, user, isLogin) {
    console.log(user.password)
    db.get("select email,passwd from user where id = ? ",id,function (err,row) {
      if (err != null){
            res.writeHead(500,{'Content-Type': 'text/plain','Content-Length': "Server error".length});
            res.end("Server error");
            console.log(err);
        }else if (typeof row == "undefined"){
            res.writeHead(500,{'Content-Type': 'text/plain','Content-Length': "Server error".length});
            res.end("Server error");
            console.log(row);
        }else{
            var responseString = JSON.stringify("password changed");
            if (row.password != user.oldpassword) {
                db.run("update user set passwd = ? where id = ?", [user.password, id]);
            } else{
                if (row.email == user.email) {
                    //
                };
            };
            res.writeHead(200,{'Content-Type': 'text/plain','Content-Length': Buffer.byteLength(responseString,'utf8')});
            res.end(responseString);
        }  
    });
}

function edit (id, db, res, user, isLogin) {
    db.get("select email,portrait,username from user where id = ? ",id,function (err,row) {
      if (err != null){
            res.writeHead(500,{'Content-Type': 'text/plain','Content-Length': "Server error".length});
            res.end("Server error");
            console.log(err);
        }else if (typeof row == "undefined"){
            res.writeHead(500,{'Content-Type': 'text/plain','Content-Length': "Server error".length});
            res.end("Server error");
            console.log(row);
        }else{
            var responseString = JSON.stringify("user profile has changed.");
            if (isLogin) {
                db.run("update user set username = ?, email = ?, portrait = ?", [user.username, user.email, user.portrait]);
            } else{

            };
            res.writeHead(200,{'Content-Type': 'text/plain','Content-Length': Buffer.byteLength(responseString,'utf8')});
            res.end(responseString);
        }  
    });
}

exports.process = process;
